#include "log.h"
#include "console.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

static uint8_t g_log_en[LOG_LV_NUM] = {0};
static char    g_log_buf[LOG_BUF_SIZE];

void log_print(uint8_t lv, char *tag, const char* format, ...)
{
    int32_t len, size;
    va_list list;
    
    if(g_log_en[lv] == 0)
        return;
    
    if(tag)
    {
        len = strlen(tag);
        memcpy(g_log_buf, tag, len);
    }else
    {
        len = 0;
    }
    
    va_start(list, format);

    size = vsnprintf(g_log_buf + len, LOG_BUF_SIZE - len, format, list);
    
    if(size >= LOG_BUF_SIZE - len)
        size = LOG_BUF_SIZE - len;
    else if(size < 0)
        size = 0;
    
    if(size)
        console_send((uint8_t *)g_log_buf, size + len);
    
    va_end(list);
}


void log_enable(uint8_t lv, uint8_t en)
{
    g_log_en[lv] = en;
}

